#include "umlmethodhighlighter.h"

UmlMethodHighlighter::UmlMethodHighlighter(QTextDocument *parent, DatatypeDictionary *dictionary) :
    UmlHighlighter(parent),
    _dataTypes(dictionary)
{
    this->_visibilityFormat.setForeground(QColor("#cd1000"));
    this->_visibilityFormat.setFontWeight(QFont::Bold);
    this->addRegex(Visibility, "public|private|protected|\\+|-|#", true);

    this->_primitiveDatatypeFormat.setForeground(QColor("#006dcd"));
    this->_primitiveDatatypeFormat.setFontWeight(QFont::Bold);
    this->addRegex(PrimitiveDatatype, this->regexForPrimitiveDatatypes() + "|void", false);

    this->_customDatatypeFormat.setForeground(QColor("#46900f"));
    this->_customDatatypeFormat.setFontWeight(QFont::Bold);
    this->addRegex(CustomDatatype, this->regexForCustomDatatypes(), false);

    this->_methodNameFormat.setForeground(QColor("#890090"));
    this->addRegex(MethodName, "(get|set)[a-zA-Z0-9]*", false);
}

QString UmlMethodHighlighter::regexForPrimitiveDatatypes() const
{
    QStringList list;
    QListIterator<XmiDatatype> iter (this->_dataTypes->datatypeList());
    while (iter.hasNext()) {
        XmiDatatype datatype = iter.next();
        if (datatype.isPrimitive()) {
            list << datatype.label();
        }
    }

    return list.join("|");
}

QString UmlMethodHighlighter::regexForCustomDatatypes() const
{
    QStringList list;
    QListIterator<XmiDatatype> iter (this->_dataTypes->datatypeList());
    while (iter.hasNext()) {
        XmiDatatype datatype = iter.next();
        if (!datatype.isPrimitive()) {
            list << datatype.label();
        }
    }

    return list.join("|");
}

void UmlMethodHighlighter::applyFormat(Type type, int index, int length)
{
    switch (type) {
    case UmlHighlighter::Visibility :
        this->setFormat(index, length, this->_visibilityFormat);
        break;
    case UmlHighlighter::CustomDatatype :
        this->setFormat(index, length, this->_customDatatypeFormat);
        break;
    case UmlHighlighter::MethodName :
        this->setFormat(index, length, this->_methodNameFormat);
        break;
    default :
        this->setFormat(index, length, this->_primitiveDatatypeFormat);
        break;
    }
}
